package com.pine.dbant.plugin.mysql.function;

import com.pine.dbant.common.jdbc.JdbcUtils;
import com.pine.dbant.common.model.EditorDbKey;
import com.pine.dbant.common.plugin.meta.JdbcMetadataAnalysisConfig;
import com.pine.dbant.common.plugin.meta.QueryMetadataParam;
import com.pine.dbant.common.plugin.meta.function.jdbc.AllMetadataEditorKeyFunction;
import com.pine.sunflower.core.utils.Strings;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xiaoyuan
 * @create 2023/3/9 9:22
 **/
public class MysqlAllMetadataEditorKeyFunction extends AllMetadataEditorKeyFunction {


    public MysqlAllMetadataEditorKeyFunction(String type) {
        super(type);
    }


    @Override
    public List<EditorDbKey> doQuery(JdbcMetadataAnalysisConfig metadataAnalysisConfig, QueryMetadataParam queryMetadataParam) {
        String showDbSql = "show databases";
        List<Map<String, Object>> dbList = JdbcUtils.getSqlRunner(metadataAnalysisConfig.getConnection()).queryList(showDbSql);
        String allTablesSql = Strings.format("  select  t1.TABLE_NAME,t1.TABLE_SCHEMA,t1.TABLE_TYPE,t1.TABLE_COMMENT  from information_schema.tables t1 \n" +
                "                left join   information_schema.COLLATION_CHARACTER_SET_APPLICABILITY  t2  on t1.table_collation = t2.collation_name \n" +
                "              \n ");
        List<Map<String, Object>> tableResult = JdbcUtils.getSqlRunner(metadataAnalysisConfig.getConnection()).queryList(allTablesSql);
        Map<String, List<Map<String, Object>>> groupTableResult = tableResult.stream().collect(Collectors.groupingBy(e -> (String) e.get("TABLE_SCHEMA")));
        final Object[] temp = new Object[1];
        EditorDbKey editorDbKey;
        List<EditorDbKey> result = new ArrayList<>();
        List<Map<String, Object>> tempList;
        for (Map<String, Object> db : dbList) {
            editorDbKey = new EditorDbKey();
            temp[0] = db.get("Database");
            if (temp[0] != null) {
                editorDbKey.setDbName(String.valueOf(temp[0]));
                tempList = groupTableResult.get(editorDbKey.getDbName());
                if (tempList != null) {
                    editorDbKey.setTables(tempList.stream().map(map -> {
                        EditorDbKey.Table table = new EditorDbKey.Table();
                        temp[0] = map.get("TABLE_NAME");
                        if (temp[0] != null) {
                            table.setTblName(String.valueOf(temp[0]));
                            temp[0] = map.get("TABLE_TYPE");
                            if (temp[0] != null) {
                                table.setType(String.valueOf(temp[0]));
                            }
                            temp[0] = map.get("TABLE_COMMENT");
                            if (temp[0] != null) {
                                table.setComment(String.valueOf(temp[0]));
                            }
                        }
                        return table;
                    }).collect(Collectors.toList()));
                }
            }
            result.add(editorDbKey);
        }
        return result;
    }
}
